// pages/index/panel/panel.js
import {
    Ble
} from "../../../utils/ble"
import {
    Protocol
} from "../../../utils/protocol"

Page({
    /**
     * 页面的初始数据
     */
    data: {
        bleStatus: "",
        macAddress: 0x00FFEECC,
        light: 0,
        tureHeight: 0,
        presetIcon: [{
                name: '1',
                icon: '/image/icon/countdown.png'
            },
            {
                name: '2',
                icon: ''
            },
            {
                name: '3',
                icon: ''
            },
            {
                name: '4',
                icon: ''
            },
            {
                name: '5',
                icon: '/image/icon/brightness_w.png'
            },
            {
                name: '6',
                icon: '/image/icon/brightness_n.png'
            },
            {
                name: '7',
                icon: '/image/icon/brightness_c.png'
            },
            {
                name: '8',
                icon: '/image/icon/brightness_b.png'
            },
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    onLoad: function (options) {
        // 微信api，可以获取页⾯的信息，用来自动获取高度并转换单位rpx
        wx.getSystemInfo({
            success: function (res) {
                console.log('devWidth: ' + res.windowWidth + ' px' + '; ' + 'devHeight: ' + res.windowHeight + ' px') // 窗口可用高度、宽度px
                let rpxScale = 750 / res.windowWidth; // rpx与px的比例，1 px相当于1 rpxMeasure rpx
                console.log('rpxScale: 1px = ' + rpxScale + ' rpx') // 最后获得转化后得rpx单位的窗口高度
                let windowHeight = (res.windowHeight * rpxScale); // 将高度乘以换算后的该设备的rpx与px的比例

                console.log('wxWdith: 750 rpx; wxHeight: ' + windowHeight + ' rpx') // 窗口可用高度、宽度rpx
            }
        })
    },
    // 蓝牙通信
    sendData(bytes) {
        Ble.server.startAdvertising({
            advertiseRequest: {
                serviceUuids: bytes
            },
            powerLevel: 'high',
            success: (res) => {
                console.log(" advertise message success ")
                this.timer = setTimeout(() => {
                    Ble.server.stopAdvertising({})
                }, 300)
            },
            fail: (error) => {
                console.log(JSON.stringify(error))
            },
            complete: () => {
                console.log(" advertise message complete ")
            }
        })
    },
    // 配对
    pairDevice(e) {
        let params = new Uint8Array(9)
        params[0] = 0x01
        params[3] = 0xFF
        params[4] = 0xEE
        params[5] = 0xCC
        let bytes = Protocol.encode(0x02, this.data.macAddress, params)
        this.sendData(bytes)
    },
    // 开灯
    turnOn(e) {
        let params = new Uint8Array(9)
        params[0] = 0x23
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },
    // 关灯
    turnOff(e) {
        let params = new Uint8Array(9)
        params[0] = 0x24
        let bytes = Protocol.encode(0x3B, this.data.macAddress, params)
        this.sendData(bytes)
    },
    // 开关状态监听
    listenerSwitch: function (e) {
        console.log('switch类型开关当前状态-----', e.detail.value);
        if (e.detail.value == true) {
            this.turnOn()
        } else {
            this.turnOff()
        }
    },

})